/*
  学习目标：创建store的推荐写法
*/

import { legacy_createStore as createStore } from 'redux';

//   2. 使用单独的常量, 维护state初始值
const initState = {
  count: 0,
  msg: '',
  title: '',
  name: 'zs',
};

// 1. 使用单独的函数, 维护reducer
function countReducer(state = initState, { type }) {
  console.warn('action  ----->  ', state.count, type);

  // 3. 使用switch语句, 替换if else语句
  switch (type) {
    case 'add':
      return { count: state.count + 1 };
    case 'des':
      return { count: state.count - 1 };
    default:
      return state;
  }
}

const store = createStore(countReducer);

const unFn = store.subscribe(() => {
  console.log('仓库数据变化会自动执行  ----->  ', store.getState());
});

store.dispatch({ type: 'add' });
store.dispatch({ type: 'des' });
store.dispatch({ type: 'des' });
store.dispatch({ type: 'des' });

unFn();

store.dispatch({ type: 'des' });
store.dispatch({ type: 'des' });
store.dispatch({ type: 'des' });
store.dispatch({ type: 'des' });
store.dispatch({ type: 'des' });
store.dispatch({ type: 'des' });
store.dispatch({ type: 'des' });
